package tech.aistar.day09;

/**
 * 本类用来演示:线程非安全.拼接效率高
 *
 * @author: success
 * @date: 2021/3/18 1:36 下午
 */
public class StringBuilderDemo {
    public static void main(String[] args) {
        //1. 创建一个StringBuilder对象
        //String类型->StringBuilder类型
        StringBuilder x = new StringBuilder("ac");
        //4. 调用拼接的方法
        x.append("abc");
        x.append("def");
        System.out.println(x);
        //2.输出容量
        System.out.println(x.capacity());//默认容量是16
        //3.输出长度
        System.out.println(x.length());//字符串的长度

        //API调用...
        //StringBuilder deleteCharAt(int index);
        StringBuilder deleteStr = x.deleteCharAt(2);
        System.out.println(x);//abdef
        System.out.println(deleteStr);//abdef

        //delete(int start, int end);//[start,end)
        x = new StringBuilder("helloWorld");
        StringBuilder strDel = x.delete(1,4);
        System.out.println(strDel);//hoWorld
        System.out.println(x);//hoWorld

        x.insert(1,"ppp");
        System.out.println(x);//hpppoWorld

        //反转
        x.reverse();
        System.out.println(x);



        //StringBuilder转成String类型
        String s = x.toString();

        String s2 = String.valueOf(x);
    }
}
